<!DOCTYPE html>
<html>
	<head>
		<title>添加/修改</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<!-- 所有的 css & js 资源 -->
		<link rel="stylesheet" href="https://unpkg.com/element-ui@2.13.0/lib/theme-chalk/index.css">
		<link rel="stylesheet" href="../../static/sa.css">
		<style>
			.uploader .el-upload {
				border: 1px dashed #d9d9d9;
				border-radius: 6px;
				cursor: pointer;
				position: relative;
			}
			.uploader .el-upload:hover {
				border-color: #409EFF;
			}
			.uploader-icon {
				font-size: 28px;
				color: #8c939d;
				width: 78px;
				height: 78px;
				line-height: 78px;
				text-align: center;
			}
			.avatar {
				width: 78px;
				height: 78px;
				display: block;
			}
		</style>
		<script src="https://unpkg.com/vue@2.6.10/dist/vue.min.js"></script>
		<script src="https://unpkg.com/element-ui@2.13.0/lib/index.js"></script>
		<script src="https://unpkg.com/jquery@3.4.1/dist/jquery.min.js"></script>
		<script src="https://www.layuicdn.com/layer-v3.1.1/layer.js"></script>
		<script src="../../static/sa.js"></script>
		<script src="../../sa-resources/router.js"></script>
	</head>
	<body>
		<div class="vue-box" style="display: none;" :style="'display: block;'">
			<!-- 参数栏 -->
			<div class="c-panel">
				<div class="c-title">添加/修改</div>
				<el-form v-if="m">
					<div class="c-item br">
						<label class="c-label">分类名称：</label>
						<el-input size="mini" v-model="m.name"></el-input>
					</div>
					<div class="c-item br">
						<label class="c-label">父级分类：</label>
						<el-select
							size="small"
						    v-model="m.parent_id"
						    filterable
						    allow-create
						    default-first-option
						    placeholder="请选择父级分类">
						    <el-option v-for="item in categoryList" :key="item.category_id" :label="item.name" :value="item.category_id"></el-option>
						  </el-select>
					</div>
					<div class="c-item br">
						<label class="c-label">分类编号：</label>
						<el-input size="mini" v-model="m.category_bn"></el-input>
					</div>
					<div class="c-item br">
						<label class="c-label">可用：</label>
						<el-select size="mini" v-model="m.is_active">
							<el-option label="是" :value="1"></el-option>
							<el-option label="否" :value="0"></el-option>
						</el-select>
					</div>
					<div class="c-item br">
						<label class="c-label">图标：</label>
						<el-upload
						  class="uploader"
						  :action="uploadUrl"
						  :show-file-list="false"
						  :on-success="handleIconSuccess"
						  :data="fileParam"
						  :before-upload="beforeUpload">
						  <img v-if="m.icon_url" :src="m.icon_url" class="avatar">
						  <i v-else class="el-icon-plus uploader-icon"></i>
						</el-upload>
					</div>
					<div class="c-item br">
						<label class="c-label">描述：</label>
						<el-input size="mini" v-model="m.intro"></el-input>
					</div>
					<div class="c-item br">
						<label class="c-label">备注：</label>
						<el-input size="mini" v-model="m.remark"></el-input>
					</div>
					<div class="c-item br">
						<label class="c-label"></label>
						<el-button size="mini" type="primary" icon="el-icon-plus" size="mini" @click="ok">确定</el-button>
					</div>
				</el-form>
			</div>
		</div>
		<script type="text/javascript">
			new Vue({
				el: '.vue-box',
				data: {
					id: sa.p('id', 0),
					m: {},
					uploadUrl: sa.cfg.api_url + router.uploadUrl,
					fileParam: {
						type: 'image'
					},
					categoryList: []
				},
				methods: {
					// 创建一个 默认Model 
					createModel: function() {
						return {
							is_active: 1,
							icon_url: ''
						};
					},
					// ok
					ok: function() {
						sa.ajax(router.updateCategory, this.m, function(res){
							sa.alert('操作成功', function(res) {
								sa.closeCurrIframe();	// 关闭本页
								parent.window.location.reload();
							}.bind(this)); 
						}.bind(this));
					},
					beforeUpload(file) {
						const isJPG = file.type === 'image/jpeg';
						const isLt2M = file.size / 1024 / 1024 < 2;
						if (!isJPG) {
							this.$message.error('上传头像图片只能是 JPG 格式!');
						}
						if (!isLt2M) {
							this.$message.error('上传头像图片大小不能超过 2MB!');
						}
						return isJPG && isLt2M;
					},
					handleIconSuccess(res, file) {
						console.log(res);
						// this.m.icon_url = URL.createObjectURL(file.raw);
						this.m.icon_url = res.data.file_url;
					}
				},
				mounted: function(){
					// 初始化数据
					sa.ajax(router.getCategoryList, {parent_id: 0, page_size: 99999}, function(res){
						this.categoryList = res.list;
					}.bind(this), {type: 'get'});
					if(this.id <= 0) {
						this.m = this.createModel();
					} else {
						sa.ajax(router.getCategory + '?id=' + this.id, function(res) {
							this.m = res;
						}.bind(this), {type: 'get'});
					}
				}
			})
		</script>
	</body>
</html>